package com.my.zxsyd.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.my.zxsyd.common.ResultEnum;
import com.my.zxsyd.entity.Admin;
import com.my.zxsyd.entity.Yonghu;
import com.my.zxsyd.exception.MyException;
import com.my.zxsyd.mapper.YonghuMapper;
import com.my.zxsyd.qpage.QPage;
import com.my.zxsyd.service.IYonghuService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.my.zxsyd.util.ResultView;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lbk
 * @since 2024-02-19
 */
@Service
public class YonghuServiceImpl extends ServiceImpl<YonghuMapper, Yonghu> implements IYonghuService {

    @Override
    public ResultView getPageList(QPage qPage, Yonghu obj) {
        IPage iPage = new Page(qPage.getOffset(), qPage.getLimit());
        QueryWrapper<Yonghu> queryWrapper=new QueryWrapper<>();
        queryWrapper.lambda().like(StringUtils.isNotBlank(obj.getNickname()), Yonghu::getNickname,obj.getNickname());
        queryWrapper.orderByDesc("ctime");
        IPage<Yonghu> mapList = this.page(iPage,queryWrapper);
        return ResultView.ok(mapList);
    }

    @Override
    public ResultView add(Yonghu obj) {
        Yonghu dto=new Yonghu();
        BeanUtils.copyProperties(obj,dto);
        dto.setCtime(new Date());
        boolean isOk=this.save(dto);
        if(!isOk){
            throw new MyException(ResultEnum.CODE_2,"新增数据异常");
        }
        return ResultView.ok();
    }

    @Override
    public ResultView del(Integer id) {
        Yonghu dto=this.getById(id);
        if(dto==null){
            throw new MyException(ResultEnum.CODE_2,"数据不存在");
        }
        boolean isOk=this.removeById(id);
        if(!isOk){
            throw new MyException(ResultEnum.CODE_2,"删除数据异常");
        }
        return ResultView.ok();
    }

    @Override
    public ResultView up(Yonghu obj) {
        Yonghu dto=this.getById(obj.getId());
        if(dto==null){
            throw new MyException(ResultEnum.CODE_2,"数据不存在");
        }
        BeanUtils.copyProperties(obj,dto);
        boolean isOk=this.updateById(dto);
        if(!isOk){
            throw new MyException(ResultEnum.CODE_2,"更新数据异常");
        }
        return ResultView.ok();
    }

    @Override
    public Yonghu login(String account, String pwd) {
        QueryWrapper<Yonghu> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("account",account);
        queryWrapper.eq("pwd",pwd);
        Yonghu yonghu=this.getOne(queryWrapper);
        return yonghu;
    }


}
